package com.parktech.sun.web.controller.management.specialtopic;

import com.parktech.sun.domain.model.Constants;
import com.parktech.sun.domain.model.specialtopic.SpecialTopicCategory;
import com.parktech.sun.domain.repository.Page;
import com.parktech.sun.domain.service.specialtopic.SpecialTopicService;
import com.parktech.sun.utils.ImageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;

/**
 * User: Think
 * Date: 11-1-12
 * Time: 下午8:02
 */
@Controller
@RequestMapping("/management/specialtopic/categories")
public class SpecialTopicCategoryController {

    private int pageSize = Constants.PAGINATION_PAGE_SIZE;

    private SpecialTopicService specialTopicService;

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Autowired
    public SpecialTopicCategoryController(SpecialTopicService specialTopicService) {
        this.specialTopicService = specialTopicService;
    }

    @RequestMapping(value = "/search", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView search(@ModelAttribute SpecialTopicSearchForm specialTopicSearchForm) {

        Page page = new Page(pageSize, specialTopicSearchForm.getPageNo());

        ModelAndView modelAndView = new ModelAndView("management/specialtopic/specialTopicCategorySearch");

        return modelAndView.addObject(specialTopicService.searchCategory(specialTopicSearchForm.getTitle(), page));
    }

    @RequestMapping(value = "/search", method = RequestMethod.DELETE)
    public String delete(@RequestParam("specialTopicCategoryIds") Long[] specialTopicCategoryIds) {
        specialTopicService.deleteAll(SpecialTopicCategory.class, specialTopicCategoryIds);
        return "redirect:search";
    }

    @ModelAttribute("specialTopicCategory")
    public SpecialTopicCategory populateModel(HttpServletRequest request) {
        String strId = request.getParameter("id");

        if (strId == null || strId.equals("-1")) {
            return new SpecialTopicCategory();
        }
        return specialTopicService.getByCategoryId(new Long(strId));
    }

    @RequestMapping(value = "/{specialTopicCategoryId}", method = RequestMethod.GET)
    public String edit(@PathVariable Long specialTopicCategoryId, Model model) {
        SpecialTopicCategory specialTopicCategory = specialTopicService.getByCategoryId(specialTopicCategoryId);
        model.addAttribute(specialTopicCategory);
        return "management/specialtopic/specialTopicCategoryForm";
    }

    @RequestMapping(method = RequestMethod.GET)
    public String create(Model model) {
        model.addAttribute(new SpecialTopicCategory());
        return "management/specialtopic/specialTopicCategoryForm";
    }

    @RequestMapping(method = RequestMethod.POST)
    public String save(@ModelAttribute @Valid SpecialTopicCategory specialTopicCategory, BindingResult result,
                       @RequestParam("image") MultipartFile image,
                       HttpServletRequest request) throws IOException {

        if (result.hasErrors()) {
            return "management/specialtopic/specialTopicCategoryForm";
        }
        specialTopicService.saveOrUpdateCategory(specialTopicCategory);
        if (image != null && !image.isEmpty()) {
            processImage(specialTopicCategory, image, request.getSession().getServletContext());
        }

        specialTopicService.saveOrUpdateCategory(specialTopicCategory);

        return "redirect:/management/specialtopic/categories/search";
    }

    private void processImage(SpecialTopicCategory specialTopicCategory, MultipartFile image, ServletContext servletContext) throws IOException {

        String specialTopicPath = servletContext.getRealPath("/upload/specialTopicCategory/" + specialTopicCategory.getId());
        File specialTopicDir = new File(specialTopicPath);
        if (!specialTopicDir.exists()) specialTopicDir.mkdirs();

        File imageFile = new File(specialTopicDir, image.getOriginalFilename());
        if (imageFile.exists()) {
            imageFile.delete();
        }
        ImageHelper.resizeFix(image.getInputStream(), imageFile, 150, 75);
        specialTopicCategory.setImageName(image.getOriginalFilename());
    }
}
